package com.yogee.topseller.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.ListRvAdapter;
import com.yogee.topseller.bean.SmallListBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.EmptyRecyclerView;
import com.yogee.topseller.view.MySinaRefreshView;
import com.yogee.topseller.view.RefreshView;

import java.util.List;

import butterknife.BindView;

public class BeginQueryActivity extends HttpActivity implements RefreshUtil.OnRefreshListener {
    @BindView(R.id.begin_query_title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.begin_query_rv)
    EmptyRecyclerView beginQueryRv;
    @BindView(R.id.bengin_query_rf_layout)
    TwinklingRefreshLayout benginQueryRfLayout;
    @BindView(R.id.id_empty_view)
    View id_empty_view;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;
    private ListRvAdapter adapter;
    private String id;
    private List<SmallListBean.ProductListBean> bean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_begin_query;
    }

    @Override
    protected void initView() {
        if (getIntent() != null) {
            id = getIntent().getStringExtra("id");
        }
        titleLayout.setTitle("闪搜结果");
        titleLayout.setActivity(this);
        iv_empty.setImageResource(R.drawable.nodata4);
        MySinaRefreshView headerView = new MySinaRefreshView(this);
        benginQueryRfLayout.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        benginQueryRfLayout.setHeaderView(headerView);
        benginQueryRfLayout.setBottomView(new RefreshView(this));
        adapter = new ListRvAdapter(this);
        beginQueryRv.setHasFixedSize(true);
        beginQueryRv.setLayoutManager(new LinearLayoutManager(this));
        beginQueryRv.setAdapter(adapter);
        beginQueryRv.setEmptyView(id_empty_view);
        beginQuery(AppUtil.getUserId(this), "0", "10", "1");
    }

    @Override
    public void onRefresh() {
        beginQuery(AppUtil.getUserId(this), "0", "10", "1");
    }

    @Override
    public void onLoad() {
        beginQuery(AppUtil.getUserId(this), String.valueOf(bean.size()), "10", "2");
    }

    private void beginQuery(String userId, String total, String count, final String isRefresh) {
        HttpManager.getInstance().beginQuery(userId, id, total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<SmallListBean>() {
            @Override
            public void onNext(SmallListBean o) {
                if (isRefresh.equals("1")) {
                    benginQueryRfLayout.finishRefreshing();
                    bean = o.getProductList();
                } else {
                    benginQueryRfLayout.finishLoadmore();
                    bean.addAll(o.getProductList());
                }
                adapter.addData(bean);
            }
        }, this));
    }
}
